package android.slc.db.core;

import java.util.LinkedHashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： slc
 * @date： 2023/07/07
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
public class DbLinkedHashMap<V> extends LinkedHashMap<String, V> {

    @Override
    public V get(Object key) {
        key = correctionKey(key);
        return super.get(key);
    }

    public V getOrDefault(Object key, V defaultValue) {
        key = correctionKey(key);
        V value = get(key);
        if(value==null){
            return defaultValue;
        }
        return value;
    }

    @Override
    public boolean remove(Object key, Object value) {
        key = correctionKey(key);
        return super.remove(key, value);
    }

    @Override
    public V remove(Object key) {
        key = correctionKey(key);
        return super.remove(key);
    }

    @Override
    public boolean containsKey(Object key) {
        key = correctionKey(key);
        return super.containsKey(key);
    }

    /**
     * 纠正Key为String类型
     *
     * @param key
     * @return
     */
    protected String correctionKey(Object key) {
        //如果为空直接返回
        if (key == null) {
            return null;
        }
        //不是String类型则toString
        if (!(key instanceof String)) {
            return key.toString();
        }
        //是String直接强转
        return (String) key;
    }
}
